﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntryEngine;
using EntryEngine.Network;
using EntryEngine.Serialize;

namespace Server.Impl
{
    class ImplIUser : ImplUserBase<T_USER>, _IUser
    {
        public ImplIUser() { }
        public ImplIUser(int uid)
        {
            InitializeByID(uid);
        }
        public ImplIUser(int uid, ImplIUser impl)
        {
            InitializeByID(uid);
            SaveBuilder = impl.SaveBuilder;
            SaveValues = impl.SaveValues;
        }
        public ImplIUser(int uid, StringBuilder builder, List<object> values)
        {
            InitializeByID(uid);
            SaveBuilder = builder;
            SaveValues = values;
        }

        protected override int OnInsert(T_USER user)
        {
            // 防止头像之类的 T_UserBase 没有但 T_USER 有的数据在注释时没有存入数据库，最常见的就是微信注册
            return _DB._T_USER.Insert(user);
        }
        /// <summary>需要更新的T_PLAYER的数据字段</summary>
        public HashSet<ET_USER> Updates = new HashSet<ET_USER>();
        /// <summary>需要更新的T_USER的字段，调用Save时统一保存</summary>
        public void Update(params ET_USER[] updates)
        {
            foreach (var item in updates)
                Updates.Add(item);
        }
        protected override void OnSave(StringBuilder builder, List<object> objs)
        {
            if (Updates.Count > 0)
            {
                _DB._T_USER.GetUpdateSQL(User, null, builder, objs, Updates.ToArray());
                Updates.Clear();
            }
        }

        T_USER _IUser.UserInfo()
        {
            // 同步部分数据
            return User;
        }
        bool _IUser.UserModifyPassword(string opass, string npass)
        {
            UserModifyPassword(opass, npass);
            return true;
        }
        bool _IUser.UserModifyPhone(string phone, string code)
        {
            UserModifyPhone(phone, code);
            return true;
        }
        bool _IUser.UserExitLogin()
        {
            UserExitLogin();
            return true;
        }
    }
}
